package day_2023_7_to_10;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @author haomin
 * @date 2023/07/18 22:24
 **/
class ListNode {
    int val;
    ListNode next;
    ListNode(int x) { val = x; }
}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode(int x) { val = x; }
}

class Node {
    int val;
    Node next;
    Node random;

    public Node(int val) {
        this.val = val;
        this.next = null;
        this.random = null;
    }
}

public class Day_7_18 {
    class Solution {
        public String replaceSpace(String s) {
            StringBuffer sb = new StringBuffer();
            for(char c : s.toCharArray()){
                sb.append(c == ' ' ? "%20" : c);
            }
            return sb.toString();
        }
    }
    class Solution2 {
        public String reverseLeftWords(String s, int n) {
            StringBuffer res = new StringBuffer(s.substring(0, n)).reverse();
            res.append(new StringBuffer(s.substring(n)).reverse());
            return res.reverse().toString();
        }
    }
    class Solution3 {
        public int[] reversePrint(ListNode head) {
            if(head == null) return new int[0];
            Deque<Integer> s = new LinkedList<>();
            while(head != null) {
                s.push(head.val);
                head = head.next;
            }
            return s.stream().mapToInt(Integer::intValue).toArray();
        }
    }
    class Solution4 {
        public ListNode reverseList(ListNode head) {
            if(head == null || head.next == null) return head;
            ListNode dummyHead = new ListNode(-1);
            ListNode cur = head;
            while(cur != null) {
                ListNode next = cur.next;
                cur.next = dummyHead.next;
                dummyHead.next = cur;
                cur = next;
            }
            return dummyHead.next;
        }
    }
    class Solution5 {
        public ListNode reverseList(ListNode head) {
            if(head == null || head.next == null) return head;
            ListNode second = head.next;
            ListNode newHead = reverseList(head.next);

            second.next = head;
            head.next = null;
            return newHead;
        }
    }
}